package net.betou.trade.main.dao.impl;

import java.util.Date;

import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.Log;
import net.betou.trade.main.dao.LogDao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository
public class LogDaoImpl extends HibernateBaseDao< Log, Integer > implements LogDao{
	
	public Pagination getPage( int category, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize ){
		Finder f = Finder.create( "from Log bean where  bean.category=:category " );
		f.setParam( "category", category );
		
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );
		
		return find(f, pageNo, pageSize);
	}

	public Log findById( Integer id ) {
		Log entity = get( id );
		return entity;
	}

	public Log save( Log bean ) {
		getSession( ).save( bean );
		return bean;
	}

	public Log deleteById( Integer id ) {
		Log entity = super.get( id );
		if( entity != null ){
			getSession( ).delete( entity );
		}
		return entity;
	}

	public int deleteBatch( Integer category, Date before ) {
		Finder f = Finder.create( "delete Log bean where 1=1" );
		if( category != null ){
			f.append( " and bean.category=:category" );
			f.setParam( "category", category );
		}
		
		if( before != null ){
			f.append( " and bean.time<=:before" );
			f.setParam( "before", before );
		}
		Query q = f.createQuery( getSession( ) );
		return q.executeUpdate( );
	}

	@Override
	protected Class< Log > getEntityClass( ) {
		return Log.class;
	}
}